package top.resty.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.resty.erp.domain.OutStorage;
import top.resty.erp.domain.Storage;
import top.resty.erp.service.OutStorageService;
import top.resty.erp.service.StorageService;
import top.resty.erp.service.UserService;
import top.resty.erp.util.LayuiJson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author : resty
 * @date : 2021年03月31 19:33
 */
@Controller
@RequestMapping("/outStorage")
public class OutStorageController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private StorageService storageService;
	
	@Autowired
	private OutStorageService outStorageService;
	
	
	
	@RequestMapping("/outIdList")
	@ResponseBody
	public LayuiJson<OutStorage> findByOutIdList(String outId,int page,int limit){
		Page<Object> pages = PageHelper.startPage(page, limit);
		LayuiJson<OutStorage> layuiJson = new LayuiJson<OutStorage>();
		
		List<OutStorage> list=outStorageService.findOutStoragesByOutId(outId);
		layuiJson.setCount((int) pages.getTotal());
		layuiJson.setData(list);
		return layuiJson;
	}
	
	
	@RequestMapping("/saveStorages")
	@ResponseBody
	public String saveStorages(@RequestBody String listStr){
		List<OutStorage> outStorages = JSONObject.parseArray(listStr, OutStorage.class);
		System.out.println("outStorages = " + outStorages);
		outStorages.forEach(outStorage -> {
			Integer count = outStorage.getCount();
			Integer id = outStorage.getStorageId();
			Storage storage = storageService.findById(id);
			int i = storage.getCount() - count;
			storageService.updateCount(i,id);
			outStorage.setOutDate(new Date());
		});
		
		int i = outStorageService.saveOutStorages(outStorages);
		// int i = 0 ;
		return i!=0?"success":"fail";
	}
	
	
	
	@RequestMapping("/getOutStorageByCondition")
	@ResponseBody
	public List<Storage> getOutStorageByCondition(String variety,String type,String standard ){
		Storage storage = new Storage();
		storage.setVariety(variety);
		storage.setType(type);
		storage.setStandard(standard);
		
		List<Storage> storages = storageService.findStorageByCondition(storage);
		return storages;
	}
	
	
	@RequestMapping("/getTelephones")
	@ResponseBody
	public List<String> getTelephones(String param){
		return userService.getTelephones(param);
	}
	
	
	@RequestMapping("/getContactNames")
	@ResponseBody
	public List<String> getContactNames (String param){
		return userService.getContactNames(param);
	}
	
	@RequestMapping("/getLocationNames")
	@ResponseBody
	public List<String> getLocationNames(String param){
		return userService.getLocationNames(param);
	}
	
	
	@RequestMapping("/add")
	public String gotoOutStoragePage(Model model){
		model.addAttribute("companies",userService.getCompanies());
		
		Storage storage = new Storage();
		List<Storage> storages = storageService.findStorageByCondition(storage);
		List<String> varieties = new ArrayList<String>();;
		storages.forEach((storage1)->{
			varieties.add(storage1.getVariety());
		});
		model.addAttribute("varieties",varieties);
		return "man/out_storage";
	}
	
	
	@RequestMapping("/list")
	public String gotoOutStorageListPage(Model model){
		
		
		model.addAttribute("companies",userService.getCompanies() );
		
		return "man/out_storage_list";
	}
	
}
